package com.hotstrip.web.api;

import com.google.common.io.ByteStreams;
import com.linecorp.bot.client.LineMessagingClient;
import com.linecorp.bot.model.action.MessageAction;
import com.linecorp.bot.model.response.BotApiResponse;
import com.linecorp.bot.model.richmenu.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * Created by idiot on 2019/7/19.
 */
@Deprecated
@RestController
public class LineBotRichMenuController {
    private static Logger logger = LoggerFactory.getLogger(LineBotRichMenuController.class);

    @Resource
    private LineMessagingClient lineMessagingClient;

    /**
     * 查询菜单列表
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/api/getRichMenuList")
    public void getRichMenuList() throws ExecutionException, InterruptedException {
        CompletableFuture<RichMenuListResponse> call = lineMessagingClient.getRichMenuList();
        RichMenuListResponse richMenuListResponse = call.get();
        logger.info("richMenu list: {}", richMenuListResponse);
    }

    /**
     * 创建菜单
     * @throws IOException
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/api/createMenu")
    public void createMenu() throws IOException, ExecutionException, InterruptedException {
        InputStream is = this.getClass().getResourceAsStream("/richmenu/request.json");
        if (is == null){
            logger.error("file do not exist...");
            return;
        }
        String data = new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8);
        logger.info("result data: {}", data);
        // RichMenu richMenu = JSON.parseObject(data, RichMenu.class);
        RichMenuBounds richMenuBounds = new RichMenuBounds(0, 0, 2500, 1686);
        MessageAction messageAction = new MessageAction("postback", "action=buy&itemid=123");
        RichMenuArea richMenuArea = new RichMenuArea(richMenuBounds, messageAction);
        RichMenu richMenu = RichMenu.builder()
                .size(RichMenuSize.FULL)
                .selected(false)
                .name("Nice richmenu")
                .chatBarText("Tap here")
                .areas(Arrays.asList(richMenuArea))
                .build();
        CompletableFuture<RichMenuIdResponse> call = lineMessagingClient.createRichMenu(richMenu);
        RichMenuIdResponse result = call.get();
        logger.info("create richMenu result is: {}", result);
    }

    /**
     * 上传菜单图片
     * @param richMenuId
     * @throws IOException
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/api/{richMenuId}/uploadRichMenuImage")
    public void uploadRichMenuImage(@PathVariable("richMenuId") String richMenuId) throws IOException, ExecutionException, InterruptedException {
        InputStream inputStream = this.getClass().getResourceAsStream("/static/richmenu.jpg");
        byte[] bytes = ByteStreams.toByteArray(inputStream);
        CompletableFuture<BotApiResponse> call = lineMessagingClient.setRichMenuImage(richMenuId, "image/jpeg", bytes);
        BotApiResponse response = call.get();
        logger.info("upload RichMenu Image result is: {}", response);
    }

    /**
     * 设置默认菜单
     * @param richMenuId
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/api/{richMenuId}/setDefaultRichMenu")
    public Object setDefaultRichMenu(@PathVariable("richMenuId") String richMenuId) throws ExecutionException, InterruptedException {
        CompletableFuture<BotApiResponse> call = lineMessagingClient.setDefaultRichMenu(richMenuId);
        BotApiResponse response = call.get();
        logger.info("upload RichMenu Image result is: {}", response);
        return response;
    }

    /**
     * 获取默认菜单信息
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping(value = "/api/getDefaultRichMenu")
    public Object getDefaultRichMenu() throws ExecutionException, InterruptedException {
        RichMenuResponse richMenuResponse = null;
        CompletableFuture<RichMenuIdResponse> call = lineMessagingClient.getDefaultRichMenuId();
        RichMenuIdResponse response = call.get();
        // 获取菜单信息
        if (response != null) {
            CompletableFuture<RichMenuResponse> callRichMenu = lineMessagingClient.getRichMenu(response.getRichMenuId());
            richMenuResponse = callRichMenu.get();
            logger.info("default RichMenu is: {}", richMenuResponse);
        }
        return richMenuResponse;
    }

}
